<?php
namespace app\index\controller;
use think\Db;
class Trade extends Base{
    protected $userId;
    public function _initialize(){
        parent::_initialize();
        $this->userId=cookie('user_id');
    }
    public function index(){
        if (request()->isPost()){
            return json(['msg' => '数据加载完成' ,'state' => -1]);
        }
        $sell=Db::name('order')->where('type','sell')->where('state=0')/*->where('user_id','<>',$this->userId)*/->select();
        $buy=Db::name('order')->where('type','buy')->where('state=0')/*->where('user_id','<>',$this->userId)*/->select();
        $this->assign(['sell' => $sell, 'buy' => $buy]);
        return view();
    }
    // 交易订单
    public function tradeOrder(){
        $order=Db::name('order')->where('user_id',$this->userId)->whereOr('buy_user_id',$this->userId)->order('add_time desc')->select();
        $this->assign([
            'order' => $order,
            'isConfirm' => Db::name('order')->where('user_id',$this->userId)->order('add_time desc')->where('state',0)->select(),
            'isPay' => Db::name('order')->where('buy_user_id',$this->userId)->order('add_time desc')->where('state',1)->select(),
            'isComplete' => Db::name('order')->where('state=2')->where('user_id',$this->userId)->order('add_time desc')/*->whereOr('buy_user_id',$this->userId)*/->select(),
        ]);
        return view('trade_order');
    }
    // 订单详情
    public function tradeDetail(){
        $id = input('id');
        $orderInfo=Db::name('order')->where('id',$id)->find();
        $this->assign([
            'orderInfo' => $orderInfo,
            'buy_user' => Db::name('users')->where('id',$orderInfo['buy_user_id'])->find(),
            'user_id' => $this->userId,
        ]);
        return view('trade_detail');
    }
    // 出售
    public function sell(){
        if (request()->isPost()){
            $trade_password=input('trade_password');
            $user=Db::name('users')->where('id',$this->userId)->find();
            $id = input('id');
            if ($user['is_real'] == 0){
                return json(['msg' => '请实名制后再出售' ,'code' => -4]);
            }
            if (strlen($user['trade_password']) != 6){
                return json(['msg' => '请设置6位交易密码','code' => -3]);
            }
            if ($trade_password != $user['trade_password']){
                return json(['msg' => '交易密码不正确' ,'code' => -1]);
            }
            $order=Db::name('order')->where('id',$id)->find();
            if ($order['state'] != 0){
                return json(['msg' => '该订单已被匹配' ,'code' => -5,]);
            }
            if ($user['money'] < $order['money']*1.25){
                return json(['msg' => 'HX余额不足,无法出售' ,'code' => -2]);
            }
            Db::name('order')->where('id',$id)->update(['buy_user_id' => $this->userId, 'state' => 1, 'match_time' => time(), // 售卖的时间
                'expire_time' => time()+3600,'buy_phone' => $user['phone'],
            ]);
            Db::name('users')->where('id',$this->userId)->setDec('money',$order['money']*1.25);
            Db::name('users')->where('id',$this->userId)->setInc('frozen_money',$order['money']*1.25);
            Db::name('message')->insert([
                'title' => '卖单提醒','content' => '有人匹配你的交易订单,请前去个人中心查看 -- 华兴数字资产','add_time' => time(),'user_id' => $this->userId,
            ]);
            return json(['msg' => '出售成功','code' => 1]);
        }
        $orderInfo=Db::name('order')->where('id',input('get.id'))->find();
        $this->assign([
            'orderInfo' => $orderInfo,
        ]);
        return view();
    }
    // 购买
    public function buy(){
        if (request()->isPost()){
            $trade_password=input('trade_password');
            $user=Db::name('users')->where('id',$this->userId)->find();
            $id = input('id');
            if ($user['is_real'] == 0){
                return json(['msg' => '请实名制后再购买' ,'code' => -4]);
            }
            if (strlen($user['trade_password']) != 6){
                return json(['msg' => '请设置6位交易密码','code' => -3]);
            }
            if ($trade_password != $user['trade_password']){
                return json(['msg' => '交易密码不正确' ,'code' => -1]);
            }
            $order=Db::name('order')->where('id',$id)->find();
            if ($order['state'] != 0){
                return json(['msg' => '该订单已被匹配' ,'code' => -5,]);
            }
            Db::name('order')->where('id',$id)->update(['buy_user_id' => $this->userId, 'state' => 1, 'match_time' => time(), // 售卖的时间
                'expire_time' => time()+3600,'buy_phone' => $user['phone'],
            ]);
            Db::name('message')->insert([
                'title' => '买单提醒','content' => '有人匹配你的交易订单,请前去个人中心查看 -- 华兴数字资产','add_time' => time(),'user_id' => $this->userId,
            ]);
            return json(['msg' => '购买成功','code' => 1]);
        }
        $orderInfo=Db::name('order')->where('id',input('get.id'))->find();
        $this->assign([
            'orderInfo' => $orderInfo,
        ]);
        return view();
    }
    // 订单完成付款操作 订单确定操作
    public function orderPayConfirm(){
        $type=input('type');
        $id=input('id');
        $order=Db::name('order')->where('id',$id)->find();
        if (is_null($order)){
            return json(['msg' => '该订单不存在','code' => -1]);
        }
        if ($type == 1){
            Db::name('order')->where('id',$id)->setField('is_pay',1);//并发送通知给卖已经支付成功了
            return json(['msg' => '已通知卖家支付成功','code' => 1]);
        }
        if ($order['expire_time'] < time()){
            Db::name('order')->where('id',$id)->setField('is_expire',1);
            return json(['msg' => '该订单已失效' ,'code' => -7]);
        }
        Db::name('order')->where('id',$id)->update(['is_confirm' => 1,'state' => 2,]);
        if ($order['type'] == 'sell'){ // 卖单
            Db::name('account_detail')->insert(['add_time' => time(), 'user_id' => $this->userId, 'money' => -$order['money']*1.25, 'remark' => '出售HX币']);
            Db::name('account_detail')->insert(['add_time' => time(),'user_id' => $order['buy_user_id'],'money' => $order['money'],'remark' => '购买HX币']);
            Db::name('users')->where('id',$this->userId)->setDec('frozen_money',$order['money']*1.25);
            Db::name('users')->where('id',$order['buy_user_id'])->setInc('money',$order['money']);
        }else{ // 买单
            Db::name('account_detail')->insert(['add_time' => time(), 'user_id' => $order['buy_user_id'], 'money' => -$order['money']*1.25, 'remark' => '出售HX币']);
            Db::name('account_detail')->insert(['add_time' => time(),'user_id' => $order['user_id'],'money' => $order['money'],'remark' => '购买HX币']);
            Db::name('users')->where('id',$order['user_id'])->setInc('money',$order['money']);
            Db::name('users')->where('id',$order['buy_user_id'])->setDec('frozen_money',$order['money']*1.25);
        }
        $arrs=[
            [
                'title' => '交易完成','content' => '您在华兴的交易订单已完成,请前去个人中心查看 -- 华兴数字资产','add_time' => time(),'user_id' => $order['user_id'],
            ],
            [
                'title' => '交易完成','content' => '您在华兴的交易订单已完成,请前去个人中心查看 -- 华兴数字资产','add_time' => time(),'user_id' => $order['buy_user_id'],
            ]
        ];
        Db::name('message')->insertAll($arrs);
        return json(['msg' => '已确定成功','code' => 1]);
    }
}